package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroBattleFormatSave
 * @description 心仪武将武将试炼布阵确定
 * @author 梁健彬
 * @since 2022-07-8
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroBattleFormatSave, classtype = ActiveMessage.ActBelovedHeroBattleFormatSaveRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroBattleFormatSave extends Command<ActiveMessage.ActBelovedHeroBattleFormatSaveRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroBattleFormatSaveRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroBattleFormatSaveResponse.Builder response = ActiveMessage.ActBelovedHeroBattleFormatSaveResponse.newBuilder();

        try {
            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();
            Result result = heroHandle.saveFormat(am, request);//保存队伍
            if(!result.equals(Result.Success)){
                send(messageSource, response, result);
                return;
            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroBattleFormatSaveResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }




}
